package yxy.cherry.battle.module.affix.buff;

import yxy.cherry.battle.Battle;
import yxy.cherry.battle.Fighter;
import yxy.cherry.battle.module.PropertyData;
import yxy.cherry.battle.module.PropertyType;
import yxy.cherry.battle.module.affix.AffixFactory;
import yxy.cherry.battle.module.affix.Buff;
import yxy.cherry.battle.module.affix.BuffType;
import yxy.cherry.battle.module.record.stream.RecordStream;
import yxy.cherry.battle.module.record.stream.RecordStream_BFA;
import yxy.cherry.battle.module.record.stream.RecordStream_BUF;
import yxy.cherry.data.bean.skill;

import java.util.ArrayList;

/**
 * 群体伤害衰减
 *
 * @ClassName Affix84OverallDamageFalloff
 * @Author jingtao-zheng
 * @Date 2022/10/3 19:04
 * @Version 1.0
 **/
@BuffType(Type = AffixFactory.BuffTypeEnum.全体属性衰减)
public class Affix84OverallAttributesFalloff extends Buff {


    public Affix84OverallAttributesFalloff(Fighter source, Fighter target, skill skilldata, int[] params) {
        super(source, target, skilldata, params);
    }

    @Override
    public boolean exec(Fighter source, Fighter target) {
        //获取第轮次
        int round = target.getContext().getRound();
        //获取buff的属性
        PropertyData propertyData = getPropertyData();
        if(propertyData == null){
            propertyData = new PropertyData();
            setPropertyData(propertyData);
        }
        /**
         * 参数1:衰减类型
         * 参数2:初始衰减万分比
         * 参数3:每回合减少的衰减
         */
        int[] params = getParams();
        //获取减少的属性类型
        PropertyType propertyType = PropertyType.forNumber(params[0]);
        //根据初始衰减万分比和轮次计算最终减少的属性万分比
        int finalAttributes = params[1] - ((round - 1) * params[2]);
        long value = 0;
        if(finalAttributes<=0){
            ArrayList<Buff> buffList = new ArrayList<>();
            buffList.add(this);
            target.removeBuff(this);
            getRecord().stream(new RecordStream_BFA(target, buffList)).record();
            Battle.battleLogger(6, "全体属性衰减：" + target.getName() + "移除了全体属性衰减buff");
            return true;
        }else{
            long attack = target.getData().getPropertyData().getValue(propertyType);
            value = attack * finalAttributes/VALUE_比率分母;
        }

        propertyData.setValue(propertyType,(~(value - 1)));
        Battle.battleLogger(6,"全体属性衰减：武将"+target.getName()
                +"#减少"+propertyType.name()+"属性"
                +"#原始攻击为："+ finalAttributes
                );
        return true;
    }

    @Override
    public RecordStream record() {
        return getRecord().stream(new RecordStream_BUF(getActor(), getActor().getBuffMap().get(this.getBuffCodeSeq())));
    }
}
